import {Component} from '@angular/core';
import {App, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {FastMaintainConfirmPage} from "../fast-maintain-confirm/fast-maintain-confirm";
import {TopTabModel} from "../../model/topTab.model";
import {ROUTER} from "../../service/router.service";
import {APARTMENTAPISERVICE} from "../../api/apartment-api.service";
import {RepairApiService} from "../../api/repair-api.service";
import {WechatUtil} from "../../utils/wechat.util";
import {ImageApiService} from "../../api/image-api.service";
import {CheckStandModel} from "../../model/checkstand.model";
import {LocalStorage} from "../../utils/localstorage.utils";
import {USER} from "../../service/user.service";
import {PageParam} from "../../api/api.service";
import {EventService} from "../../service/event.service";

/**
 * Generated class for the FastMaintainPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-fast-maintain',
  templateUrl: 'fast-maintain.html',
})
export class FastMaintainPage {
  pageParam:PageParam;
  hasMore:boolean = true;

  items:Array<TopTabModel> = [];
  currentPageIndex:number = 0;
  rouInfo:any = null;
  reqData:any = {
    houseId: '',
    content: '',
    detail: '',
    image: [],
  };
  houseList:any = [];
  maintainDetail:any = [];

  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              public app:App,
              private modalCtrl:ModalController,
              private ROUTER:ROUTER,
              public WX:WechatUtil,
              private apartmentApi:APARTMENTAPISERVICE,
              private repairApi:RepairApiService,
              private imageApi:ImageApiService,
              private ls:LocalStorage,
              private USER:USER,
              public eventService:EventService,) {
    this.rouInfo = this.navParams.get('id');
    this.items.push(new TopTabModel({id: 0, name: '申请维修', isSelect: true, func: this.tabChange}));
    this.items.push(new TopTabModel({id: 1, name: '我的维修', func: this.tabChange}));
    if (this.rouInfo != -1) {
      this.items[0].isSelect = false;
      this.items[1].isSelect = true;
      this.currentPageIndex = this.items[1].id;

    }
  }

  tabChange = v=> {
    for (let item of this.items) {
      item.isSelect = false
    }
    v.isSelect = true;
    this.currentPageIndex = v.id;
  }

  ionViewDidLoad() {
    this.loadMyHouse();
    this.doRefresh();
    this.eventService.subscribe(EventService.REFRESH_USER, ()=> {
      this.doRefresh();
    })

  }

  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.repairApi.getMyRepair(v => {
      let list:any = [];
      for (let item of v.content) {
        let data:any = {
          id: item.id,
          buzItem: item.buzItem,
          buzProduct: item.buzProduct,
          buzType: item.buzType,
          comment: item.comment,
          commentScore: item.commentScore,
          content: item.content,
          createTime: item.createTime.split(' ', 1)[0],
          image: item.image == null ? null : JSON.parse(item.image),
          itemName: item.itemName,
          needFee: item.needFee,
          payMoney: item.payMoney,
          payStatus: item.payStatus,
          payTime: item.payTime,
          payType: item.payType,
          productName: item.productName,
          providerId: item.providerId,
          providerName: item.providerName,
          providerPhone: item.providerPhone,
          providerTime: item.providerTime,
          repairFee: item.repairFee,
          status: item.status,
          statusName: item.statusName,
          uuid: item.uuid
        }
        list.push(data);
      }
      this.maintainDetail = list;
      this.hasMore = !v.last;
      console.log(this.maintainDetail);
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.repairApi.getMyRepair(v => {
      this.hasMore = !v.last;
      let list:any = [];
      for (let item of v.content) {
        let data:any = {
          id: item.id,
          buzItem: item.buzItem,
          buzProduct: item.buzProduct,
          buzType: item.buzType,
          comment: item.comment,
          commentScore: item.commentScore,
          content: item.content,
          createTime: item.createTime.split(' ', 1)[0],
          image: item.image == null ? null : JSON.parse(item.image),
          itemName: item.itemName,
          needFee: item.needFee,
          payMoney: item.payMoney,
          payStatus: item.payStatus,
          payTime: item.payTime,
          payType: item.payType,
          productName: item.productName,
          providerId: item.providerId,
          providerName: item.providerName,
          providerPhone: item.providerPhone,
          providerTime: item.providerTime,
          repairFee: item.repairFee,
          status: item.status,
          statusName: item.statusName,
          uuid: item.uuid
        }
        list.push(data);
      }
      this.maintainDetail = this.maintainDetail.concat(list);
      infiniteScroll.complete();
    }, this.pageParam);
  }

  goToRating(item:any) {
    this.ls.set('commentNo',item.uuid);
    this.ROUTER.goRatingPage();
  }

  chooseImage() {
    this.WX.chooseAndUploadImage(1, v=> {
    }, mediaId=> {
      this.imageApi.wxImageUpload2oss(mediaId, (res:any)=> {
        this.reqData.image[0] = res.saveUrl;
        this.USER.toast("图片上传成功");
      })
    })
  }

  loadMyHouse() {
    this.apartmentApi.getMyRentingHouse({}, (res:any)=> {
      this.houseList = res;
      console.log(this.houseList);
    })
  }

  getMyRepair() {
    let reqData:any = {
      size: 99,
      page: 0
    };
    this.repairApi.getMyRepair(reqData, (res:any)=> {
      // status: 1-待沟通，2-待上门，4-待评价，8-已评价
      let list:any = [];
      for (let item of res.content) {
        let data:any = {
          id: item.id,
          buzItem: item.buzItem,
          buzProduct: item.buzProduct,
          buzType: item.buzType,
          comment: item.comment,
          commentScore: item.commentScore,
          content: item.content,
          createTime: item.createTime.split(' ', 1)[0],
          image: item.image == null ? null : JSON.parse(item.image),
          itemName: item.itemName,
          needFee: item.needFee,
          payMoney: item.payMoney,
          payStatus: item.payStatus,
          payTime: item.payTime,
          payType: item.payType,
          productName: item.productName,
          providerId: item.providerId,
          providerName: item.providerName,
          providerPhone: item.providerPhone,
          providerTime: item.providerTime,
          repairFee: item.repairFee,
          status: item.status,
          statusName: item.statusName,
          uuid: item.uuid
        }
        list.push(data);
      }
      this.maintainDetail = list;
      console.log(this.maintainDetail);
    })
  }

  submit() {
    console.log(this.reqData);
    this.repairApi.submitRepair(this.reqData, (res:any)=> {
      let profileModal = this.modalCtrl.create(FastMaintainConfirmPage, this.reqData, {showBackdrop: true});
      profileModal.present();
    })
  }

  goCheckstand(item:any) {
    // console.log(item);
    let checkstand = new CheckStandModel(item.uuid, '维修费用', item.repairFee, item.id, ROUTER.FASTMAINTAIN);
    // console.log(checkstand);
    this.ls.setObject('checkstand', checkstand);
    this.ROUTER.go(ROUTER.CHECK);
  }

  ionViewDidEnter() {
    document.title = '我的维修';
  }
}
